# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request, FormRequest

class CsdnloginSpider(scrapy.Spider):
    name = 'CSDNLogin'
    allowed_domains = ['passport.csdn.net','edu.csdn.net']
    # start_urls = ['https://passport.csdn.net/account/verify']
    header = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.5.2 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.2"}

    def start_requests(self):
        return [Request("https://passport.csdn.net/account/verify",
                        meta={"cookiejar":1},callback=self.parse)]

    def parse(self, response):
        data = {
            "username":"",
            "password":"",
            "_eventId":"submit"
        }
        return [FormRequest.from_response(response,
                                          meta={"cookiejar": response.meta["cookiejar"]},
                                          headers=self.header,
                                          formdata=data,
                                          callback = self.next
                                          )]

    def next(self, response):
        yield Request("http://edu.csdn.net/mycollege",meta={"cookiejar":True}, callback=self.mycollege)

    def mycollege(self, response):
        print(response.body)
        print(response.xpath("//div[@class='item_main']/h1/a/text()").extract())